#version 400

in vec3 LightIntensity;
in vec3 Position;
in vec3 Normal;
in vec2 DiffuseTexCoord;
in vec2 DirtTexCoord;

uniform float UseDiffuseTexture;
uniform float UseDirtTexture;
uniform float DirtPercentage;
uniform sampler2D DiffuseTex;
uniform sampler2D DirtTex;

out vec4 FragColor;

void main() 
{
	vec4 diffuseTexColor = texture( DiffuseTex, DiffuseTexCoord );
	vec4 dirtTexColor = texture( DirtTex, DirtTexCoord );
	
	float dirtiness = (dirtTexColor.r + dirtTexColor.g + dirtTexColor.b) / 3;
	
	float dirtyMix = DirtPercentage * cos ( dirtiness * 3.14159 / 2 );

	if (UseDiffuseTexture > 0.5 && UseDirtTexture > 0.5) {
		FragColor = (diffuseTexColor * (1 - dirtyMix) + dirtTexColor * dirtyMix) * vec4 (LightIntensity, 1.0);
	} else if (UseDiffuseTexture > 0.5)  {
		FragColor = diffuseTexColor * vec4 (LightIntensity, 1.0);
	} else if (UseDirtTexture > 0.5) {
		FragColor = (dirtTexColor * dirtyMix) + (vec4 (1.0) * (1 - dirtyMix)) * vec4 (LightIntensity, 1.0);
	} else {
		FragColor = vec4 (LightIntensity, 1.0);
	}
}